package com.example.cet4try.activity;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.example.cet4try.R;
import com.example.cet4try.Utils.Tools;
import com.example.cet4try.Utils.WordData;
import com.example.cet4try.myDialog.WordDetailsDialog;

public class ActivityEnglishChineseTest extends BaiduTTSActivity{
    private Button button_cn1;
    private Button button_cn2;
    private Button button_cn3;
    private Button button_cn4;
    private int dui;
//    private String[] data;
    private WordData wordData;
    private boolean endflag = false,wrongFlag = false;
//    private TextToSpeech textToSpeech; // TTS对象
//    private LinearLayout answer_s;
    private WordDetailsDialog wordDetailsDialog;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_encn_test);

        button_cn1 = findViewById(R.id.button_cn1);
        button_cn2 = findViewById(R.id.button_cn2);
        button_cn3 = findViewById(R.id.button_cn3);
        button_cn4 = findViewById(R.id.button_cn4);
        TextView english_show1 = findViewById(R.id.english_show1);
        TextView english_show2 = findViewById(R.id.english_show2);
//        answer_s = findViewById(R.id.answer_s);
        button_cn1.setOnClickListener(new button_cn_Onclick());
        button_cn2.setOnClickListener(new button_cn_Onclick());
        button_cn3.setOnClickListener(new button_cn_Onclick());
        button_cn4.setOnClickListener(new button_cn_Onclick());
        Button next = findViewById(R.id.next_en);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reStart();
            }
        });

        wordDetailsDialog = new WordDetailsDialog("英中测试");
        wordDetailsDialog.setPlaySound(new WordDetailsDialog.PlaySound() {
            @Override
            public void speak_d() {
                speak();
            }
        });

        SharedPreferences preferences = getSharedPreferences("App_Shared_Data", 0);
        username = preferences.getString("username","");

        int id = Tools.MyRandom(1,4493);
        wordData = Tools.souID(id);
        Tools.AddUserData(username,wordData.getMid(),"learned");

        // 按钮控制开始朗读
        Button speechBtn = findViewById(R.id.langdu);
        speechBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                speak();

            }
        });

        Button btn_rs = findViewById(R.id.button5);
        btn_rs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.AddUserData(username,wordData.getMid(),"already");
                reStart();
            }
        });

        english_show1.setText(wordData.getEnglish());//显示英文
        english_show2.setText(Tools.getYinbiao(wordData.getPhonetic()));//显示音标

        dui = Tools.MyRandom(1,4);
        setShowText(dui,Tools.GetRandomdata(wordData.getChinese()));//设置正确中文选项

        for (int i = 1;i<=4;i++){
            if (i!=dui){
                int iderror = Tools.MyRandom(1,4493);
                String str = Tools.GetRandomdata(Tools.souID(iderror).getChinese());
                setShowText(i,str);
            }

        }

    }


    private void setShowText(int index,String str){
        switch (index){
            case 1:button_cn1.setText(str);break;
            case 2:button_cn2.setText(str);break;
            case 3:button_cn3.setText(str);break;
            case 4:button_cn4.setText(str);break;
            default:break;
        }
    }

    private void speak() {
        super.speak(wordData.getEnglish());
    }

    private class button_cn_Onclick implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (endflag){
                return;
            }
            AddTimes();
            Button button = findViewById(R.id.button_cn1);
            int btn = 0;
            switch (view.getId()){
                case R.id.button_cn1:btn=1;button =  findViewById(R.id.button_cn1);break;
                case R.id.button_cn2:btn=2;button =  findViewById(R.id.button_cn2);break;
                case R.id.button_cn3:btn=3;button =  findViewById(R.id.button_cn3);break;
                case R.id.button_cn4:btn=4;button =  findViewById(R.id.button_cn4);break;
                default:break;
            }

            //english_show1.setCompoundDrawables(null, null, yesicon, null);
            //Drawable noicon = getResources().getDrawable(R.drawable.no);
            //noicon.setBounds(0,0,150,150);

            if (dui==btn){
                endflag = true;
                if (!wrongFlag)
                    AddRight();
                Drawable yesicon = getResources().getDrawable(R.drawable.yes);
                yesicon.setBounds(0,0,150,150);
                button.setCompoundDrawables(null, null, yesicon, null);
                wordDetailsDialog.setData(wordData);
                wordDetailsDialog.show(getSupportFragmentManager(),"WordDetailsDialog");
            }
            else {
                wrongFlag = true;
                Animation shake = AnimationUtils.loadAnimation(ActivityEnglishChineseTest.this,R.anim.shake);
                button.startAnimation(shake);
                Tools.AddUserData(username,wordData.getMid(),"wrong");

            }


        }
    }
}
